package ltd.jdsoft.cute.spring.web.request;

import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.Range;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

/**
 * 附件上传的请求参数
 * 
 * @author zhengzhq
 *
 */
public class FileRequest {

  // 附件类型
  @Range(min = 1, max = 5, message = "附件类型非法")
  private int type;
  // 附件对象
  @NotNull(message = "附件不能为空，请检查后重新上传")
  private CommonsMultipartFile[] files;

  public static enum Type {

    TMP(0, "tmp", "临时目录"),
    AVATAR(1, "avatar", "用户头像"),
    ID_CARD(2, "idcard", "用户身份证"),
    BANK_CARD(3, "bankcard", "用户银行卡"),
    BIZ_LICENSE(4, "bizlicense", "用户营业执照"),
    AD(5, "ad", "广告");

    // 类型
    private int type;
    // 附件所在的目录名称，upload的一级子目录
    private String folder;
    // 附件描述
    private String desc;

    private Type(int type, String folder, String desc) {
      this.type = type;
      this.folder = folder;
      this.desc = desc;
    }

    public int type() {
      return this.type;
    }

    public String folder() {
      return this.folder;
    }

    public String desc() {
      return this.desc;
    }

    /**
     * 根据type获取对应的枚举类型
     * 
     * @param type
     * @return
     */
    public static Type getByType(int type) {
      for (Type self : Type.values()) {
        if (self.type() == type) {
          return self;
        }
      }

      return null;
    }
  }

  /**
   * @return the type
   */
  public int getType() {
    return type;
  }

  /**
   * @return the files
   */
  public CommonsMultipartFile[] getFiles() {
    return files;
  }

  /**
   * @param type the type to set
   */
  public void setType(int type) {
    this.type = type;
  }

  /**
   * @param files the files to set
   */
  public void setFiles(CommonsMultipartFile[] files) {
    this.files = files;
  }
}
